﻿using Dapper;
using Dapper.Contrib.Extensions;
using MySql.Data.MySqlClient;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace OPC.DataBase.DAL
{
    public class BaseDAL<T> where T:class,new()
    {
        protected readonly string CONNECTSTRING = ConfigurationManager.ConnectionStrings["MySQL"].ConnectionString;

        /// <summary>
        /// MySQL数据库查询方法
        /// </summary>
        /// <param name="sql"></param>
        /// <returns></returns>
        public List<T> Load(string sql)
        {
            using(IDbConnection DB=new MySqlConnection(CONNECTSTRING))
            {
                var result = DB.Query<T>(sql).ToList();
                return result;
            }
        }

        /// <summary>
        /// MySQL数据库更新
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public bool Update(T entity)
        {
            using (IDbConnection DB = new MySqlConnection(CONNECTSTRING))
            {
                var result=DB.Update(entity);
                return result;
            }
        }

        /// <summary>
        /// MySQL数据库插入
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public long Insert(T entity)
        {
            using (IDbConnection DB = new MySqlConnection(CONNECTSTRING))
            {
                var result = DB.Insert(entity);
                return result;
            }
        }

        /// <summary>
        /// MySQL数据库删除
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public bool Delete(T entity)
        {
            using (IDbConnection DB = new MySqlConnection(CONNECTSTRING))
            {
                var result = DB.Delete(entity);
                return result;
            }
        }
    }
}
